package com.alipay.app.groupmeal.standard.offline.manager;

import com.alipay.app.groupmeal.standard.offline.DbService;
import com.alipay.app.groupmeal.standard.offline.bean.FkBean;
import com.alipay.app.groupmeal.standard.offline.bean.FkBeanDao;
import com.alipay.app.groupmeal.standard.offline.bean.OrderBean;
import com.alipay.app.groupmeal.standard.offline.bean.OrderBeanDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Date;
import java.util.List;

public class FkBeanManager {
    private FkBeanDao mDao;


    public static FkBeanManager getInstance() {
        FkBeanManager.DataManagerHolder.mInstance.mDao
                = DbService.getDaoSession().getFkBeanDao();
        return FkBeanManager.DataManagerHolder.mInstance;
    }

    private static class DataManagerHolder {
        private static final FkBeanManager mInstance = new FkBeanManager();
    }

    public long insert(FkBean bean) {

        return mDao.insert(bean);
    }

    public void deleteAll() {
        mDao.deleteAll();
    }

    //批量插入
    public void insertInTx(List<FkBean> fkBeanList) {
        mDao.insertInTx(fkBeanList);
    }

    public void insertInTxForDelete(List<FkBean> fkBeanList) {
        long count = mDao.count();
        if (count > 0) {
            mDao.deleteAll();
        }
        int step = 30;
        for (int i = 0; i < Math.ceil(fkBeanList.size() / (step + 0.0)); i++) {
            int endPointer = Math.min((i + 1) * step, fkBeanList.size());//列表结束指针   math.min--->比较两值，返回最小值
            //获取指定列表区间，math.min--->比较两值，返回最小值
            mDao.insertInTx(fkBeanList.subList(i * step, endPointer));
        }

    }

    public FkBean getFkByCardSn(String cardsn) {
        QueryBuilder<FkBean> queryBuilder = mDao.queryBuilder();
        return queryBuilder
                .where(FkBeanDao.Properties.Cardsn.eq(cardsn))
                .build()
                .unique();
    }

    public FkBean getFkByAliUserId(String aliUid) {
        QueryBuilder<FkBean> queryBuilder = mDao.queryBuilder();
        return queryBuilder
                .where(FkBeanDao.Properties.AliUserId.eq(aliUid))
                .build()
                .unique();
    }
}
